package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_configuration;

import cn.com.api.service.ApiT_configurationService;

import cn.com.util.*;
import cn.com.util.*;

public class T_configurationBaseController {

	private static Logger logger = Logger
			.getLogger(T_configurationBaseController.class);

	@Autowired
	private ApiT_configurationService apiT_configurationService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_configuration-list")
	public ModelAndView t_configuration_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_configuration_list.jsp");

		List<ApiT_configuration> t_configurations = apiT_configurationService
				.findT_configurations();
		mv.addObject("t_configurations", t_configurations);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_configuration-add-pre")
	public ModelAndView t_configuration_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_configuration_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_configuration t_configuration = apiT_configurationService
					.findT_configurationById(Integer.valueOf(id));
			mv.addObject("t_configuration", t_configuration);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_configuration-add")
	public void t_configuration_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String fieldname = request.getParameter("fieldname");
		String fieldvalue = request.getParameter("fieldvalue");
		String remark = request.getParameter("remark");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_configuration t_configuration = new ApiT_configuration();

			t_configuration.setFieldname(fieldname);
			t_configuration.setFieldvalue(fieldvalue);
			t_configuration.setRemark(remark);

			apiT_configurationService.insertT_configuration(t_configuration);
		}
		// edit
		else {
			ApiT_configuration t_configuration = apiT_configurationService
					.findT_configurationById(Integer.valueOf(id));

			t_configuration.setFieldname(fieldname);
			t_configuration.setFieldvalue(fieldvalue);
			t_configuration.setRemark(remark);

			apiT_configurationService.updateT_configuration(t_configuration);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_configuration-delete")
	public void t_configuration_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_configurationService.deleteT_configuration(Integer
				.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
